package com.boco.wmworkflow.system.action;

import com.boco.core.web.BaseAjaxAction;
import com.boco.transnms.server.bo.ibo.workflow.IReceiptSMSAlertsBO;
import com.boco.transnms.server.bo.base.BoName;
import com.boco.transnms.server.bo.base.BoHomeFactory;
import com.boco.transnms.common.dto.base.BoActionContext;
import com.boco.transnms.common.dto.Applyer;
import javax.servlet.http.HttpServletRequest;
import com.boco.transnms.common.dto.base.BoQueryContext;
import java.util.List;
import com.boco.transnms.common.dto.base.DboCollection;
import java.util.ArrayList;
import java.util.Map;
import com.boco.transnms.client.model.base.IBoCommand;
import com.boco.transnms.common.dto.base.DataObjectList;
import com.boco.transnms.server.bo.helper.misc.SecurityBOHelper;
import com.boco.transnms.client.model.base.BoCmdFactory;
import java.util.HashMap;
import com.boco.transnms.common.dto.Organization;
import com.boco.transnms.common.dto.SheetFilterInfo;
import com.boco.transnms.common.dto.ReplySmsInfo;
import com.boco.transnms.server.bo.ibo.traph.IAttempSheetReceiptSMSAlertsBO;
import org.apache.commons.lang.StringUtils;

/**
 * <p>Title: </p>
 *
 * <p>Description: WebMaster4.x</p>
 *
 * <p>Copyright: Copyright (c) 2005</p>
 *
 * <p>Company: BOCO</p>
 *
 * @author not attributable
 * @version 4.2.x
 */
public class ReceiptSMSAlertsAjaxAction  extends BaseAjaxAction {
    public ReceiptSMSAlertsAjaxAction() {
    }

    public Applyer addApplyer(HttpServletRequest request, Map conMap){
        Applyer dto = new Applyer();
        dto.setUserName((String)conMap.get("userName"));
        dto.setRelateedDepCuid((String)conMap.get("userDep"));
        dto.setUserTel((String)conMap.get("userTel"));
        dto.setEmail((String)conMap.get("userEmail"));
        dto.setUserType(Long.parseLong((String)conMap.get("userType")));
        return getReceiptSMSAlertsBO().addApplyer(new BoActionContext(),dto);
    }

    public Applyer modifyApplyer(HttpServletRequest request, Map conMap){
        Applyer dto = new Applyer();
        dto.setUserName((String)conMap.get("userName"));
        dto.setRelateedDepCuid((String)conMap.get("userDep"));
        dto.setUserTel((String)conMap.get("userTel"));
        dto.setEmail((String)conMap.get("userEmail"));
        dto.setCuid((String)conMap.get("cuid"));
        dto.setObjectId((String)conMap.get("objectId"));
        getReceiptSMSAlertsBO().modifyApplyer(new BoActionContext(),dto);
        return dto;
    }

    public void delApplyer(HttpServletRequest request, String cuid){
        getReceiptSMSAlertsBO().delApplyer(new BoActionContext(),cuid);
    }

    public Applyer getApplyer(HttpServletRequest request, String cuid){
        return getReceiptSMSAlertsBO().getApplyerByCuid(new BoActionContext(),cuid);
    }

    public List getApplyerListByQuery(HttpServletRequest request, String offset, String fetchSize, Map conMap){
        List reList = new ArrayList();
        BoQueryContext queryContext = initQueryContext(offset, fetchSize, true);
        DboCollection dbos = getReceiptSMSAlertsBO().getApplyerListByQuery(queryContext,conMap);
        dealApplyList(reList,dbos);
        return reList;
    }

    private void dealApplyList(List reList, DboCollection applyList){
        if(applyList!=null && applyList.size()>0){
            List list = new ArrayList();
            Applyer applyer = null;
            for(int i=0;i<applyList.size();i++){
                applyer =(Applyer)applyList.getAttrField(Applyer.CLASS_NAME,i);
                list.add("addRow(\"" + applyer.getAttrString(Applyer.AttrName.cuid)+ "\",\"" + applyer.getAttrString(Applyer.AttrName.userName)
                                 + "," + applyer.getAttrString(Applyer.AttrName.relateedDepCuid) + "," + applyer.getAttrString(Applyer.AttrName.userTel)
                                 + "," + applyer.getAttrString(Applyer.AttrName.email) + "\"," + i + ")");
            }
            reList.add(0, String.valueOf(applyList.getCountValue()));
            reList.add(1, String.valueOf(applyList.getPageSize()));
            reList.add(2, String.valueOf(applyList.getStartPageNo()));
            reList.add(3, list);
        }
    }

    public String addSheetFilterInfo(HttpServletRequest request, Map conMap){
        SheetFilterInfo dto = new SheetFilterInfo();
        dto.setFilterName((String)conMap.get("fiterName"));
        dto.setApplyUser((String)conMap.get("applyer"));
        dto.setVpName((String)conMap.get("vpTrueName"));
        String sheetType = (String)conMap.get("sheetType");
        if(sheetType!=null && !sheetType.equals("")){
            dto.setSheetType(Long.parseLong(sheetType));
        }
        String urgent = (String)conMap.get("urgent");
        if(urgent!=null && !urgent.equals("")){
            dto.setUrgent(Long.parseLong(urgent));
        }
        dto.setCreater((String)conMap.get("creater"));
        dto.setMsReceiver((String)conMap.get("msReceiver"));
        SheetFilterInfo sheetFilterInfo = getReceiptSMSAlertsBO().addSheetFilterInfo(new BoActionContext(),dto);
        return spellSheetFilterRow(sheetFilterInfo);
    }

    private String spellSheetFilterRow(SheetFilterInfo sheetFilterInfo){
        if(sheetFilterInfo!=null){
           return "addRow(\""+sheetFilterInfo.getAttrString(SheetFilterInfo.AttrName.cuid)+ "\",\"" + sheetFilterInfo.getAttrString(SheetFilterInfo.AttrName.filterName)
                             + "," + sheetFilterInfo.getAttrString("applyerName") + "," + sheetFilterInfo.getVpName()
                             + "," + sheetFilterInfo.getAttrString("sheetTypeName") +"," + sheetFilterInfo.getAttrString("urgentName") +
                                "," + sheetFilterInfo.getAttrString("createrName") +"," + sheetFilterInfo.getAttrString("msReceiverName")+ "\"," +0+ ")";
        }
        return null;
    }

    public String modifySheetFilterInfo(HttpServletRequest request, Map conMap){
        SheetFilterInfo dto = new SheetFilterInfo();
        dto.setFilterName((String)conMap.get("fiterName"));
        dto.setApplyUser((String)conMap.get("applyer"));
        dto.setVpName((String)conMap.get("vpTrueName"));
        String sheetType = (String)conMap.get("sheetType");
        if(sheetType!=null && !sheetType.equals("")){
            dto.setSheetType(Long.parseLong(sheetType));
        }
        String urgent = (String)conMap.get("urgent");
        if(urgent!=null && !urgent.equals("")){
            dto.setUrgent(Long.parseLong(urgent));
        }
        dto.setCreater((String)conMap.get("creater"));
        dto.setMsReceiver((String)conMap.get("msReceiver"));
        dto.setObjectId((String)conMap.get("objectId"));
        dto.setCuid((String)conMap.get("cuid"));
        SheetFilterInfo sheetFilterInfo = getReceiptSMSAlertsBO().modifySheetFilterInfo(new BoActionContext(),dto);
        return spellSheetFilterRow(sheetFilterInfo);
    }

    public void delSheetFilterInfo(HttpServletRequest request, String cuid){
        getReceiptSMSAlertsBO().delSheetFilterInfo(new BoActionContext(),cuid);
    }

    public Map getSheetFilterInfo(HttpServletRequest request, String cuid){
        SheetFilterInfo sheetFilterInfo = getReceiptSMSAlertsBO().getSheetFilterInfoByCuid(new BoActionContext(),cuid);
        Map map = new HashMap();
        map.put("filterName",sheetFilterInfo.getAttrString(SheetFilterInfo.AttrName.filterName));
        map.put("applyerName",sheetFilterInfo.getApplyUser()+"@"+sheetFilterInfo.getAttrString("applyerName"));
        map.put("sheetType",sheetFilterInfo.getSheetType());
        map.put("urgent",sheetFilterInfo.getUrgent());
        map.put("vpName", sheetFilterInfo.getVpName()+"@"+sheetFilterInfo.getVpName());
        map.put("creater",sheetFilterInfo.getCreater());
        map.put("msReceiverName",sheetFilterInfo.getMsReceiver()+"@"+sheetFilterInfo.getAttrString("msReceiverName"));
        map.put("objectId",sheetFilterInfo.getObjectId());
        return map;
    }

    public List getSheetFilterInfoListByQuery(HttpServletRequest request, String offset, String fetchSize, Map conMap){
        BoQueryContext queryContext = initQueryContext(offset, fetchSize, true);
        List dbos = getReceiptSMSAlertsBO().getSheetFilterInfoListByQuery(queryContext,conMap);
        dealSheetFilterInfoList(dbos);
        return dbos;
    }

    private void dealSheetFilterInfoList(List dbos){
        if(dbos!=null && dbos.size()==4){
            List sheetFilterInfoList = (List)dbos.get(3);
            List rowList = new ArrayList();
            if(sheetFilterInfoList!=null){
               SheetFilterInfo sheetFilterInfo = null;
               for(int i=0;i<sheetFilterInfoList.size();i++){
                  sheetFilterInfo = (SheetFilterInfo)sheetFilterInfoList.get(i);
                  rowList.add("addRow(\"" + sheetFilterInfo.getAttrString(SheetFilterInfo.AttrName.cuid)+ "\",\"" + sheetFilterInfo.getAttrString(SheetFilterInfo.AttrName.filterName)
                             + "," + sheetFilterInfo.getAttrString("applyerName") + "," + sheetFilterInfo.getVpName()
                             + "," + sheetFilterInfo.getAttrString("sheetTypeName") +"," + sheetFilterInfo.getAttrString("urgentName") +
                                "," + sheetFilterInfo.getAttrString("createrName") +"," + sheetFilterInfo.getAttrString("msReceiverName")+ "\"," +i+ ")");

               }
            }
            dbos.remove(3);
            dbos.add(3,rowList);
        }
    }

    public ReplySmsInfo getReplySmsInfo(HttpServletRequest request){
        return getAttempSheetReceiptSMSAlertsBO().getReplySmsInfo(new BoActionContext());
    }

    public void updateReplySmsInfo(HttpServletRequest request,Map conMap){
        ReplySmsInfo replySmsInfo = new ReplySmsInfo();
        String replyFilterCon = (String)conMap.get("replyFilterCon");
        if(!StringUtils.isEmpty(replyFilterCon)){
            replySmsInfo.setReplyFilterCondition(replyFilterCon);
        }
        String unReplyFilterCon = (String)conMap.get("unReplyFilterCon");
        if(!StringUtils.isEmpty(unReplyFilterCon)){
            replySmsInfo.setUnreplyFilterCondition(unReplyFilterCon);
        }
        String undoAlarmTime = (String)conMap.get("undoAlarmTime");
        if(!StringUtils.isEmpty(undoAlarmTime)){
            Long undoTime = Long.parseLong(undoAlarmTime);
            replySmsInfo.setUndoAlarmTime(undoTime);
        }
        String unReplyAlarmTime = (String)conMap.get("unReplyAlarmTime");
        if(!StringUtils.isEmpty(unReplyAlarmTime)){
            Long unReplyTime = Long.parseLong(unReplyAlarmTime);
            replySmsInfo.setUnreplyAlarmTime(unReplyTime);
        }
        getAttempSheetReceiptSMSAlertsBO().updateReplySmsInfo(new BoActionContext(),replySmsInfo);
    }

    private IAttempSheetReceiptSMSAlertsBO getAttempSheetReceiptSMSAlertsBO(){
       IAttempSheetReceiptSMSAlertsBO  bo= (IAttempSheetReceiptSMSAlertsBO) BoHomeFactory.getInstance().getBO(BoName.AttempSheetReceiptSMSAlertsBO);
       return bo;
    }

    private IReceiptSMSAlertsBO getReceiptSMSAlertsBO(){
       IReceiptSMSAlertsBO  bo= (IReceiptSMSAlertsBO) BoHomeFactory.getInstance().getBO(BoName.ReceiptSMSAlertsBO);
       return bo;
    }
}
